new Vue({
    el: '#main',
    data: () => ({
        cotypename: [],//公司分类
        countryname: [],//国家
        provinces: [],//省份
        city: [],//城市
        town: [],//镇
        managename: [],//业务员
        linkarr: [],//联系人
        bankarr: [],//银行
        number: {},
        data: {
            linkdata: '',//联系人
            bankdata: '',//银行
            type_id: '',//公司分类值
            saler_id: '',//业务员值
            name: '',//公司名称值
            address: {
                country_id: '',//国家选择的值
                province_id: '',//省份选择的值
                city_id: '',//城市选择的值
                town_id: '',//乡镇选择的值
            },
            addr: '',//详细地址的值
            zipcode: '',//邮编的值
            statuss: 'NORMAL',//状态的值
            attrval: {
                select: [],
                text: []
            },
            works: '',
            remarks: '',
            act: $('#act').val(),
            co_id: $('#company_id').val()
        },
        lang: '',
        attrarr: {
            select: [],
            text: []
        },
        linkpage: 1,
        bankpage: 1,
        pageSize:2,
        is_sub: true
    }),
    mounted(){
        if ($('#company_id').val()) {
            var geturl = "/admin/customer/companyedit?company_id=" + $('#company_id').val() + '&getcompany&status=' + $('#act').val();
        } else {
            var geturl = "/admin/customer/companyadd?getcompany&status=" + $('#act').val();
        }
        $.ajax({
            url: geturl,
            data: {
                getcompany: ''
            },
            type: 'post',
            success: (data) => {
                data = JSON.parse(data)
                this.cotypename = data.cotypename
                this.countryname = data.countryname
                this.managename = data.managename
                this.lang = data.lang
                this.number = data.works
                if (data.info) {
                    this.data.type_id = data.info.type_id
                    this.data.saler_id = data.info.saler_id
                    this.data.works = data.info.works
                    this.data.name = data.info.name
                    this.data.remarks = data.info.remarks
                    this.data.address.country_id = data.info.country_id
                    var getadd = async () => {
                        await this.selchangefun('provinces', this.data.address.country_id)
                        this.data.address.province_id = data.info.province_id
                        await this.selchangefun('city', this.data.address.province_id)
                        this.data.address.city_id = data.info.city_id
                        await this.selchangefun('town', this.data.address.city_id)
                        this.data.address.town_id = data.info.town_id
                    }
                    getadd()
                    data.info.typeselect.forEach(element => {
                        if (element.is_text) {
                            this.attrarr.select.push(element)
                            if (data.info.coattrdata.length)
                                data.info.coattrdata.forEach((item) => {
                                    if (item.coattr_id == element.id) {
                                        this.data.attrval.select.push({
                                            key: element.content,
                                            id: element.id,
                                            val: item.content
                                        })
                                    }
                                })
                            else {
                                this.data.attrval.select.push({key: element.content, id: element.id, val: 0})
                            }
                        } else {
                            this.attrarr.text.push(element)
                            if (data.info.coattrdata.length)
                                data.info.coattrdata.forEach((item) => {
                                    if (item.coattr_id == element.id) {
                                        this.data.attrval.text.push({key: element.id, val: item.content})
                                    }
                                })
                            else {
                                this.data.attrval.text.push({key: element.id, val: ''})
                            }
                        }
                    });
                    this.data.addr = data.info.addr
                    this.data.zipcode = data.info.zipcode
                    this.data.statuss = data.info.status
                }
            }
        })
        $('#linkman_Modal').on('hide.bs.modal', () => {
            this.getlinkfun()
        })
        $('#bank_Modal').on('hide.bs.modal', () => {
            this.getbank()
        })
    },
    methods: {
        //获取对应的地区的值
        selchangefun(type, val, key){
            return new Promise((res, rej) => {
                $.ajax({
                    url: '/admin/api/getprovinces',
                    type: 'post',
                    data: {
                        tables: type,
                        id: val
                    },
                    success: (data) => {
                        data = JSON.parse(data)
                        switch (type) {
                            case 'provinces':
                                this.provinces = data
                                this.city = []
                                this.town = []
                                this.data.address.provinces = 0
                                this.data.address.city = 0
                                this.data.address.town = 0
                                break;
                            case 'city':
                                this.city = data
                                this.town = []
                                this.data.address.city = 0
                                this.data.address.town = 0
                                break;
                            case 'town':
                                this.town = data
                                this.data.address.town = 0
                                break;
                        }
                        res()
                        /* this[key]=data
                         this.data.address[key]=0 */
                    }
                })
            })

        },
        //获取对应的属性值
        changegsfun(){
            $.ajax({
                url: '/admin/api/getcoattr',
                type: 'post',
                data: {
                    cotype: this.data.type_id,
                    lang: this.lang
                },
                success: (data) => {
                    data = JSON.parse(data)
                    this.attrarr.select = []
                    this.data.attrval.select = []
                    this.attrarr.text = []
                    this.data.attrval.text = []
                    data.forEach(element => {
                        if (element.is_text) {
                            this.attrarr.select.push(element)
                            this.data.attrval.select.push({key: element.id, val: 0})
                        } else {
                            this.attrarr.text.push(element)
                            this.data.attrval.text.push({key: element.id, val: ''})
                        }
                    });
                }
            })
        },
        //切换标签
        changestatus(statuss){
            this.data.statuss = statuss
        },
        //点击银行
        getbank(){
            var action = $('#act').val();
            $.ajax({
                url: '/admin/customer/' + action,
                data: {
                    status: 'bank',
                    company_id: $('#company_id').val(),
                    page: this.bankpage,
                    pageSize: this.pageSize
                },
                type: 'post',
                success: (data) => {
                    data = JSON.parse(data)
                    this.bankarr = data.data
                    if(data.size / this.pageSize>1){
                        $('#paginationbank').show()
                        this.Resetnav(this.bankpage,data.size, 'bank', 'bankpage')
                    }else{
                        $('#paginationbank').hide()
                    }
                }
            })
        },
        //点击联系人
        getlinkfun(){
            var action = $('#act').val();
            $.ajax({
                url: '/admin/customer/' + action,
                data: {
                    status: 'linkman',
                    company_id: $('#company_id').val(),
                    page: this.linkpage,
                    pageSize: this.pageSize
                },
                type: 'post',
                success: (data) => {
                    data = JSON.parse(data)
                    this.linkarr = data.data
                    if(data.size / this.pageSize>1){
                        $('#paginationlink').show()
                        this.Resetnav(this.linkpage,data.size, 'link', 'linkpage')
                    }else{
                        $('#paginationlink').hide()
                    }
                }
            })
        },
        Resetnav(pageCurrent, pageSum, type, pagename) {
            //数据总条数
            var myPageCount = pageSum;
            //一页显示多少条
            var myPageSize = this.pageSize;
            //总页数
            var countindex = myPageCount % myPageSize > 0 ? (myPageCount / myPageSize) + 1 : (myPageCount / myPageSize);
            $.jqPaginator('#pagination'+type, {
                totalPages: parseInt(countindex),
                //最多显示多少页
                visiblePages: 5,
                //默认显示第几页
                currentPage: pageCurrent,
                first: '<li class="first"><a href="javascript:;">'+langcon.page_home+'</a></li>',
                prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>'+langcon.previous_page+'</a></li>',
                next: '<li class="next"><a href="javascript:;">'+langcon.next_page+'<i class="arrow arrow3"></i></a></li>',
                last: '<li class="last"><a href="javascript:;">'+langcon.last_page+'</a></li>',
                page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
                all:'<li class="page"><a href="javascript:;">'+parseInt(countindex)+' / '+this.page+'PG , '+this.pageSize+' /PG</a></li>',
                onPageChange:(num, type)=>{
                    if (type == "change") {
                        this[pagename]=num
                    }
                }
            });
        },
        //联系人的删除
        dellink(id, index){
            $.ajax({
                url: '/admin/customer/liaisondel?linkid=' + id,
                type: 'post',
                success: (data) => {
                    data = JSON.parse(data)
                    change_mes_type('.alert',data.status)
                    if (data.status != 1) {
                        $('.alert').show()
                        $('.alert p').html('*' + data.msg + '<br>')
                        //toastr.warning(res.msg);
                    }
                    else {
                        $('.alert').show()
                        $('.alert p').html('*' + data.msg + '<br>')
                        if(this.linkpage > Math.ceil(this.linkarr.length-1 / this.pageSize)){
                            if(this.linkpage>1){
                                this.linkpage--
                            }
                        }
                        this.getlinkfun()
                    }

                }
            })
        },
        //银行的删除
        delbank(id, index){
            $.ajax({
                url: '/admin/customer/bankdel?bankid=' + id,
                type: 'post',
                success: (data) => {
                    data = JSON.parse(data)
                    change_mes_type('.alert',data.status)
                    if (data.status != 1) {
                        $('.alert').show()
                        $('.alert p').html('*' + data.msg + '<br>')
                        //toastr.warning(res.msg);
                    }
                    else {
                        $('.alert').show()
                        $('.alert p').html('*' + data.msg + '<br>')
                        if(this.bankpage > Math.ceil(this.bankarr.length-1 / this.pageSize)){
                            if(this.bankpage>1){
                                this.bankpage--
                            }
                        }
                        this.getbank()
                        //this.bankarr.splice(index, 1)
                    }

                }
            })
        },
        //提交
        subfun(){
            this.data.linkdata = this.linkarr;
            this.data.bankdata = this.bankarr;
            if (this.is_sub) {
                //this.is_sub=false
                $.ajax({
                    url: '/admin/customer/' + $('#act').val(),
                    type: 'post',
                    data: this.data,
                    success: (data) => {
                        data = JSON.parse(data)
                        change_mes_type('.alert',data.status)
                        if (data.status==1) {
                            this.is_sub = false
                            $('.alert').show()
                            $('.alert p').html('*' + data.msg + '<br>')
                            //toastr.info(data.msg)
                        } else {
                            $('.alert').show()
                            $('.alert p').html('*' + data.msg + '<br>')
                            //toastr.warning(data.msg);
                        }
                    }
                })
            }
        },
        //返回列表页
        returnlist(){
            $.ajax({
                url: '/admin/api/isbanklink?status',
                type: 'post',
                success: (data) => {
                    if (data.code == 1) {
                        $('#breaklist').modal('show')
                    } else {
                        location.href="/admin/customer/customercompany"
                    }
                }
            })
        }
    },
    created(){
        this.getbank();
        this.getlinkfun();
    },
    watch: {
        //监听分页
        linkpage: function () {
            this.getlinkfun()
        },
        bankpage: function () {
            this.getbank()
        }
    }
})
$('.savebtn').click(function () {
    $.ajax({
        url: '/admin/api/isbanklink?isexist',
        type: 'post',
        success: function (data) {
            history.go(-1)
        }
    })
})